var JS_MODULE = function () {
    var owner = this;
    owner.dom_tag = "module";
    owner.dom_edit = $("#" + owner.dom_tag + "_div_add_edit");
    owner.dom_table = $("#" + owner.dom_tag + "_table");
    owner.currentId = null;
    owner.currentRow = 0;
    owner.map_data = {};
    owner.data_sign = [{text: "仅超管读写", value: 0}, {text: "仅普管可写", value: 1}];
    owner.data_level = [{text: "一级菜单", value: 1}, {text: "二级菜单", value: 2}, {text: "业务应用", value: 3}];
    owner.data_module = null;

    owner.dom_table.treegrid().treegrid({
        toolbar: [{
            iconCls: "icon-reload",
            handler: function () {
                owner.getPage();

            }
        }, {
            iconCls: "icon-edit",
            handler: function () {
                if (owner.dom_table.treegrid("getChecked").length != 1) {
                    $.messager.alert("提示信息", "必须仅选择一条数据进行编辑！");
                    return;
                }
                var row = owner.dom_table.treegrid("getChecked")[0];
                owner.currentId = row.itemId;
                owner.dom_edit.window({
                    "iconCls": "icon-edit",
                    "title": "编辑功能模块"
                }).window("open").window("hcenter");
                owner.resetSaveOrEdit();
            }
        }, {
            iconCls: "icon-add",
            handler: function () {
                owner.currentId = null;
                owner.dom_edit.window({
                    "iconCls": "icon-add",
                    "title": "添加功能模块"
                }).window("open").window("hcenter");
                owner.resetSaveOrEdit();
            }
        }, {
            iconCls: "icon-remove",
            handler: function () {
                var rows = owner.dom_table.treegrid("getChecked");
                if (rows.length < 1) {
                    $.messager.alert("提示信息", "必须选择至少一条数据！");
                    return;
                }
                $.messager.confirm("提示信息", "确定删除这些数据?", function (isDel) {
                    if (isDel) {
                        var ids = new Array();
                        for (var i = 0; i < rows.length; i++) {
                            ids.push(rows[i].itemId);
                        }
                        ;
                        $.ajax({
                            type: "post",
                            url: "/manager/system/module/del",
                            cache: false,
                            dataType: "json",
                            data: {
                                "moduleIds": ids
                            }
                        }).done(function (result) {
                            if (result.isTrue) {
                                for (var i = 0; i < rows.length; i++) {
                                    owner.dom_table.treegrid("remove", rows[i].itemId);
                                }
                                ;
                            } else {
                                $.messager.alert("提示信息", result.message);
                            }
                        }).fail(function () {
                            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
                        });
                    }
                });
            }
        }],
        rowStyler: function (row) {
            if (row.p2 == "仅超管读写") {
                return "background-color:#778899;color:#fff";
            }
        }
    });

    owner.formatItem = function (obj) {
        var item = {
            "itemId": obj.moduleId,
            "p1": obj.moduleName,
            "p2": obj.moduleSignName,
            "p3": obj.moduleLevelName,
            "p4": obj.moduleIndex,
            "p5": obj.moduleUrl,
            "addTime": obj.addTime.date(),
            "_parentId": obj.moduleFather
        };
        if (obj.moduleLevelName == "二级菜单") {
            item.state = "closed";
        }
        return item;
    };

    owner.getPage = function () {
        $.ajax({
            type: "get",
            url: "/manager/system/module/list",
            cache: false,
            dataType: "json"
        }).done(function (result) {
            if (result.isTrue) {
                var data = new Array();
                owner.map_data = {};
                owner.data_module = new Array();
                if (result.items.length == 0) {
                    $.messager.alert("提示信息", "暂无数据！");
                    return;
                }
                for (var i = 0; i < result.items.length; i++) {
                    var obj = result.items[i];
                    owner.map_data[obj.moduleId] = obj;
                    if (obj.moduleLevelName != "业务应用") {
                        owner.data_module.push({text: obj.moduleName, value: obj.moduleId});
                    }
                    data.push(owner.formatItem(obj));
                }
                owner.dom_table.treegrid("loadData", {
                    "total": result.items.length,
                    "rows": data
                });
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        });
    };

    owner.resetSaveOrEdit = function () {
        $("#" + owner.dom_tag + "_ipt_p2").combobox("loadData", owner.data_sign);
        $("#" + owner.dom_tag + "_ipt_p3").combobox("loadData", owner.data_level);
        $("#" + owner.dom_tag + "_ipt_p5").combobox("loadData", owner.data_module);
        if (owner.currentId == null) {
            owner.dom_edit.find(".easyui-textbox").textbox("clear")
                .end().find(".easyui-combobox").combobox("clear")
                .end().find(".easyui-combotree").combotree("clear")
                .end().find(".easyui-numberbox").numberbox("clear")
                .end().find(".textbox").val("");
        } else {
            var item = owner.map_data[owner.currentId];
            $("#" + owner.dom_tag + "_ipt_p1").textbox("setValue", item.moduleName);
            $("#" + owner.dom_tag + "_ipt_p2").combobox("setValue", item.moduleSign);
            $("#" + owner.dom_tag + "_ipt_p3").combobox("setValue", item.moduleLevel);
            $("#" + owner.dom_tag + "_ipt_p4").numberbox("setValue", item.moduleIndex);
            $("#" + owner.dom_tag + "_ipt_p5").combobox("setValue", item.moduleFather);
            $("#" + owner.dom_tag + "_ipt_p6").textbox("setValue", item.moduleUrl);
        }
    };

    owner.saveOrEdit = function (dom) {
        dom.linkbutton("disable");
        var isAllValid = new Array();
        owner.dom_edit.find(".easyui-textbox").each(function () {
            if (!$(this).textbox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        owner.dom_edit.find(".easyui-numberbox").each(function () {
            if (!$(this).numberbox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        owner.dom_edit.find(".easyui-combobox").each(function () {
            if (!$(this).combobox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        if (isAllValid.length > 0) {
            $.messager.alert("提示信息", isAllValid.join(",") + " 输入不正确！");
            dom.linkbutton("enable");
            return;
        }
        $.ajax({
            type: "post",
            url: "/manager/system/module/addoredit",
            cache: false,
            dataType: "json",
            data: {
                "moduleId": owner.currentId,
                "moduleName": $("#" + owner.dom_tag + "_ipt_p1").textbox("getValue"),
                "moduleSign": $("#" + owner.dom_tag + "_ipt_p2").combobox("getValue"),
                "moduleLevel": $("#" + owner.dom_tag + "_ipt_p3").combobox("getValue"),
                "moduleIndex": $("#" + owner.dom_tag + "_ipt_p4").numberbox("getValue"),
                "moduleFather": $("#" + owner.dom_tag + "_ipt_p5").combobox("getValue"),
                "moduleUrl": $("#" + owner.dom_tag + "_ipt_p6").textbox("getValue")
            }
        }).done(function (result) {
            if (result.isTrue) {
                var item = result.tag;
                if (owner.currentId == null) {
                    owner.dom_table.treegrid("append", {
                        parent: item.moduleFather,
                        data: [owner.formatItem(item)]
                    });
                } else {
                    owner.dom_table.treegrid("update", {
                        id: item.moduleId,
                        row: owner.formatItem(item)
                    });
                }
                owner.map_data[item.moduleId] = item;
                if (item.moduleLevelName != "业务应用") {
                    owner.data_module.push({text: item.moduleName, value: item.moduleId});
                }
                owner.dom_edit.window("close");
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            dom.linkbutton("enable");
        });
    };


};